package com.qxt.prize.facades;

import com.qxt.prize.forms.PrizeForm;
import com.qxt.prize.models.Prize;
import com.qxt.prize.services.PrizeService;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import top.moirae.core.AbstractEither;
import top.moirae.core.ErrorMessage;
import top.moirae.repository.PaginationData;
import top.moirae.repository.PaginationForm;
import top.moirae.security.forms.IdsForm;
import top.moirae.security.models.Session;
import top.moirae.security.services.SessionService;

/**
 * @author Chan
 **/
@Named
public class PrizeFacade {
    private final PrizeService prizeService;
    private final SessionService sessionService;

    @Inject
    public PrizeFacade(PrizeService prizeService, SessionService sessionService) {
        this.prizeService = prizeService;
        this.sessionService = sessionService;
    }

    public AbstractEither<ErrorMessage, Long> create(Session session, PrizeForm.Create form) {
        return sessionService.ifHasPermission(session.getId(), Prize.Rules.CREATE, () -> prizeService.create(session.getUserId(), form));
    }

    public AbstractEither<ErrorMessage, Prize> get(long id) {
        return prizeService.get(id);
    }

    public AbstractEither<ErrorMessage, Long> update(Session session, Long id, PrizeForm.Update form) {
        return sessionService.ifHasPermission(session.getId(), Prize.Rules.UPDATE, () -> prizeService.update(session.getUserId(), id, form));
    }

    public AbstractEither<ErrorMessage, Long> changePublishState(Session session, Long id) {
        return sessionService.ifHasPermission(session.getId(), Prize.Rules.UPDATE, () -> prizeService.changePublishState(id));
    }

    public AbstractEither<ErrorMessage, PaginationData<Prize>> list(PrizeForm.Search form, PaginationForm paginationForm) {
        return prizeService.list(form, paginationForm);
    }

    public AbstractEither<ErrorMessage, Integer> delete(Session session, IdsForm ids) {
        return sessionService.ifHasPermission(session.getId(), Prize.Rules.DELETE, () -> prizeService.delete(ids));
    }
}

